<template>
	<view class="sport-type-selector" v-if="show">
		<view class="type-mask" @tap="cancel">
		</view>
		<view class="type-container" :class="{ 'type-show': show }">
			<!-- 顶部操作栏 -->
			<view class="header">
				<text class="cancel" @tap="cancel">取消</text>
				<text class="title">活动种类</text>
				<text class="confirm" @tap="confirm">确定</text>
			</view>

			<!-- 类型列表 -->
			<view class="type-list">
				<view class="type-item" 
					v-for="(item, index) in sportTypes" 
					:key="index"
					:class="{ 'selected': selectedType && selectedType.dictValue === item.dictValue }" 
					@tap="selectType(item)"
				>
					<text class="type-name">{{ item.dictLabel }}</text>
					<text class="check-icon" v-if="selectedType && selectedType.dictValue === item.dictValue">✓</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listSportType
	} from '@/api/youqiu/activity.js'

	export default {
		name: 'SportTypeSelector',
		props: {
			show: {
				type: Boolean,
				default: false
			},
			value: { // 添加 value prop 用于支持 v-model
				type: Object,
				default: null
			}
		},
		data() {
			return {
				sportTypes: [],
				selectedType: null
			}
		},
		watch: {
			show(newVal) {
				if (newVal) {
					this.getSportTypes()
					// 如果有初始值，设置选中项
					if (this.value) {
						this.selectedType = this.value
					}
				}
			},
			value(newVal) {
				this.selectedType = newVal
			}
		},
		methods: {
			getSportTypes() {
				listSportType('sys_sport_type').then(res => {
					console.log('sportTypes', res)
					this.sportTypes = res.data
				}).catch(err => {
					console.error('获取运动类型失败：', err)
					uni.showToast({
						title: '获取运动类型失败',
						icon: 'none'
					})
				})
			},
			// 选择类型
			selectType(type) {
				this.selectedType = type
				// 如果想要点击即选中并关闭，可以取消注释下面的代码
				// this.$emit('select', type)
				// this.$emit('update:show', false)
			},
			// 取消选择
			cancel() {
				this.selectedType = this.value // 恢复到初始选择
				this.$emit('update:show', false)
			},
			// 确认选择
			confirm() {
				if (this.selectedType) {
					this.$emit('select', this.selectedType)
					this.$emit('input', this.selectedType) // 支持 v-model
				}
				this.$emit('update:show', false)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.sport-type-selector {
		.type-mask {
			position: fixed;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			background: rgba(0, 0, 0, 0.4);
			z-index: 998;
		}

		.type-container {
			position: fixed;
			bottom: -100%;
			left: 0;
			right: 0;
			background-color: #fff;
			border-radius: 20rpx 20rpx 0 0;
			z-index: 999;
			transition: all 0.3s ease;

			&.type-show {
				bottom: 0;
			}

			.header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 30rpx;
				border-bottom: 1px solid #eee;

				.cancel {
					color: #666;
				}

				.title {
					font-weight: bold;
				}

				.confirm {
					color: #ff6b6b;
				}
			}

			.type-list {
				max-height: 600rpx;
				overflow-y: auto;
				padding: 20rpx;

				.type-item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 30rpx 20rpx;
					border-bottom: 1px solid #eee;

					.type-name {
						font-size: 28rpx;
						color: #333;
					}

					.check-icon {
						color: #07C160;
						font-size: 40rpx;
					}

					&.selected {
						background-color: #f8f8f8;
						
						.type-name {
							color: #07C160;
						}
					}

					&:active {
						background-color: #f0f0f0;
					}
				}
			}
		}
	}
</style>